package com.contacts.controller;

import com.contacts.context.BaseContext;
import com.contacts.pojo.Category;
import com.contacts.pojo.Contact;
import com.contacts.result.Result;
import com.contacts.service.ContactService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@Slf4j
@Api(tags = "联系人接口")
public class ContactController {
    @Autowired
    private ContactService contactService;

    /**
     * 根据ID删除联系人
     * @param id
     * @return
     */
    @ApiOperation("删除联系人")
    @DeleteMapping("/contact/delete/{id}")
    public Result deleteContact(@PathVariable Integer id){
        contactService.deleteContact(id);
        return Result.success("删除成功");
    }

    /**
     * 添加联系人
     * @param contact
     * @return
     */
    @ApiOperation("添加联系人")
    @PostMapping("/contact/add")
    public Result addContact(@RequestBody Contact contact){
        Integer userId = BaseContext.getCurrentId();
        contactService.addContact(contact,userId);
        return Result.success("添加联系人成功");
    }

    /**
     * 修改联系人
     * @param contact
     * @return
     */
    @ApiOperation("修改联系人")
    @PutMapping("/contact/update")
    public Result updateContact(@RequestBody Contact contact){
        log.info("修改联系人, 联系人id: {}", contact.getCtId());
        Integer userId = BaseContext.getCurrentId();
        contactService.updateContact(contact,userId);
        return Result.success("修改成功");

    }

    /**
     * 根据ID查询联系人
     * @param id
     * @return
     */
    @GetMapping("/contact/{id}")
    public Result getById(@PathVariable Integer id){
        Contact contact = contactService.getById(id);
        return Result.success(contact);
    }

    /**
     * 联系人统计分析
     * @param id
     * @return
     */
    @GetMapping("/user/total/{id}")
    @ApiOperation("联系人统计分析")
    public Result getTotal(@PathVariable Long id){
        List<Category> total=contactService.getTotal(id);
        return Result.success(total);
    }


}
